<#import "../template.ftl" as t/>
<@t.html ; part>
    <#if part="head">
        <link href="<@spring.url "/resources/plugins/datatables/dataTables.bootstrap.css"/>" rel="stylesheet">
        <link href="<@spring.url "/resources/plugins/datatables/responsive.dataTables.min.css"/>" rel="stylesheet">
    </#if>
    <#if part="body">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title"></h3>
            </div>
            <div class="panel-body">
                <div class="pad-btm form-inline">
                    <div class="row">
                        <div class="col-sm-6 table-toolbar-left">
                            <select name="dt-basic_length" aria-controls="dt-basic" class="form-control input-sm" id="year">
                                <#list 2020..2030 as i>
                                    <option value="${i}" <#if year?? && year==i>selected="selected"</#if>>${i}</option>
                                </#list>
                            </select>
                            <select name="dt-basic_length" aria-controls="dt-basic" class="form-control input-sm" id="month">
                                <#list 1..12 as j>
                                    <option value="${j}" <#if month?? && month==j>selected="selected"</#if>>${j}</option>
                                </#list>
                            </select>
                            <button id="demo-btn-addrow" class="btn btn-purple" onclick="search()">搜索</button>
                            &nbsp;&nbsp;
                            <button id="demo-btn-addrow" class="btn btn-purple" onclick="$('#import').modal('show')"> 导入</button>
                            <button id="demo-btn-addrow" class="btn btn-purple" onclick="saleDeviation()"> 统计销售月度偏差</button>
                        </div>
                        <div class="col-sm-6 table-toolbar-right">
                        </div>
                    </div>
                </div>
                <table id="dt-basic" class="table table-striped table-bordered"
                       cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>公司名称</th>
                        <th>销售姓名</th>
                        <th class="min-tablet">预估电量(兆瓦时)</th>
                        <th class="min-desktop">实际电量(兆瓦时)</th>
                        <th class="min-desktop">用户电费节省（元）</th>
                        <th class="min-desktop">价差</th>
                        <th class="min-desktop">偏差</th>
                        <th class="min-desktop">代理费</th>
                        <th class="min-desktop">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <#if monthData?? && (monthData?size>0)>
                        <#list monthData as data>
                            <tr>
                                <td>${data_index+1}</td>
                                <td>${(data.companyInfo.name)!}</td>
                                <td>${(data.user.name)!}</td>
                                <td>${(data.elecConsumption)!}</td>
                                <td>${(data.elecActual)!}</td>
                                <td>${(data.saveElecNum)!}</td>
                                <td>${(data.priceDifference)!}</td>
                                <td>${(data.elecConsumption-data.elecActual)/data.elecConsumption}</td>
                                <td>${(data.agencyFee)!}</td>
                                <td>
                                    <a href="javascript:void(0)" onclick="editData(${data.id})">修改</a>
                                    <a href="javascript:void(0)" onclick="deleteData(${data.id})">删除</a>
                                </td>
                            </tr>
                        </#list>
                    </#if>
                    </tbody>
                </table>
            </div>
        </div>

    </#if>
    <#if part="modal">
        <!--修改记录-->
        <div class="modal fade" id="editData" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">修改</h4>
                    </div>
                    <div class="modal-body">
                    </div>
                    <div class="modal-footer">
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="userFile" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">其他资料</h4>
                    </div>
                    <div class="modal-body">
                    </div>
                    <div class="modal-footer">
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="import">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">导入信息</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-lg-2 fieldTit">上传</div>
                            <div class="col-lg-10"><input type="file" class="form-control" id="import_file"></div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-primary" id="import_save">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </#if>

    <#if part="foot_script">
        <script src="<@spring.url "/resources/plugins/datatables/jquery.dataTables.js"/>"></script>
        <script src="<@spring.url "/resources/plugins/datatables/dataTables.bootstrap.js"/>"></script>
        <script src="<@spring.url "/resources/plugins/datatables/dataTables.responsive.min.js"/>"></script>
        <script src="<@spring.url "/resources/js/tables-datatables.js"/>"></script>
        <script type="text/javascript">
            //$(document).ready( function() {
            $('#dt-basic').dataTable( {
                "fnDrawCallback":function (settings) {
                    $("#dt-basic thead tr th:first").removeClass("sorting_asc");
                    $("#dt-basic tbody tr td:first").removeClass("sorting_asc");
                },
                "aoColumnDefs":[{"bSortable":false, "aTargets":[0]}]
            });
            //});
            function editData(dataId) {
                $.get('<@spring.url "/manage/monthData/edit" />',
                    {id:dataId},
                    function (data) {
                        $("#editData").find(".modal-body").html(data);
                        $("#editData").modal("show");
                    });
            }

            function updateData() {
                var companyName = $.trim($("#companyName").val());
                if (companyName == "") {
                    alert("请填入公司名称");
                    return false;
                }
                var userName = $.trim($("#userName").val());
                if (userName == "") {
                    alert("请填入销售手机号码");
                    return false;
                }
                var saveElecNum = $.trim($("#saveElecNum").val());
                if (saveElecNum == "") {
                    alert("请填入用户节约电费");
                    return false;
                }
                var elecConsumption = $.trim($("#elecConsumption").val());
                if (elecConsumption == "") {
                    alert("请填入预估电量");
                    return false;
                }
                var elecActual = $.trim($("#elecActual").val());
                if (elecActual == "") {
                    alert("请填入实际电量");
                    return false;
                }
                var priceDifference = $.trim($("#priceDifference").val());
                if (priceDifference == "") {
                    alert("请填入价差");
                    return false;
                }
                var agencyFee = $.trim($("#agencyFee").val());
                if (agencyFee == "") {
                    alert("请填入代理费");
                    return false;
                }
                var editYear = $.trim($("#editYear").val());
                if (editYear == "") {
                    alert("请填入年份");
                    return false;
                }
                var editMonth = $.trim($("#editMonth").val());
                if (editMonth == "") {
                    alert("请填入月份");
                    return false;
                }
                var id = $("#updateId").val();
                $.post('<@spring.url "/manage/monthData/update" />',
                    {id:id, companyName:companyName, userName:userName, saveElecNum:saveElecNum,
                        elecConsumption:elecConsumption, elecActual:elecActual, priceDifference:priceDifference,
                        agencyFee:agencyFee, year:editYear, month:editMonth},
                    function (data) {
                        if(data.code==1){
                            alert("生成成功！");
                            window.location.reload();
                        } else {
                            alert(data.message);
                        }
                    });

            }

            function saleDeviation() {
                var year = $("#year").val();
                var month = $("#month").val();
                $.post('<@spring.url "/manage/monthData/generateUserData" />',
                    {year:year, month:month},
                    function (data) {
                        if(data.code==1){
                            alert("统计成功！");
                            window.location.reload();
                        } else {
                            alert(data.message);
                        }
                    });
            }

            function search() {
                var year = $("#year").val();
                var month = $("#month").val();
                window.location.href="<@spring.url '/manage/monthData/search/"+year+"/"+month+"'/>";
            }



            $("#import_save").click(function () {
                var file = $("#import_file")[0].files[0];
                var formData = new FormData();
                formData.append("file", file);
                $.ajax({
                    type: 'post',
                    url: "<@spring.url "/manage/monthData/import"/>",
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(data) {
                        alert(data.message);
                        if (data.code!=-1){
                            $("#import").modal("hide");
                            window.location.reload();
                        }
                    }
                });
            });

        </script>

    </#if>
</@t.html>